package org.me.Slide.Adapter;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import org.me.Slide.AlbumGridActivity;
import org.me.Slide.R;
import org.me.Slide.Util.FileUtil;
import org.me.Slide.Util.XMLUtil;
import org.me.Slide.data.Albums;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.MonthDisplayHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AlbumListAdapter extends BaseAdapter {
	
	private Context mContext 	= null;
	private LayoutInflater mInflater 	= null;
	List<Albums> mAlbums 		= null;
	private Bitmap mBitmap 		= null;
	private String filename 	= null;
	private byte[] byteBuffer = new byte[128];
	
	public AlbumListAdapter(Context context)
	{
		mContext 	= context;
		mAlbums 	= new ArrayList<Albums>();
		mAlbums 	= XMLUtil.ParseDataFromXMLToList(context);
		mInflater = LayoutInflater.from(context);
		
	}
	private class ViewHolder
	{
		TextView 	mTextView;
		ImageView 	mImageView;
	}
	
	public View getView(final int position, View view, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if(view == null)
		{
			holder = new ViewHolder();
			view = mInflater.inflate(R.layout.album_list_activity_item,null);
			holder.mTextView = (TextView)view.findViewById(R.id.textView);
			holder.mImageView = (ImageView)view.findViewById(R.id.albums_list_item_image_view);
			view.setTag(holder);
		}
		else
		{
			holder = (ViewHolder)view.getTag();
		}
		
		holder.mTextView.setTag(position);
		holder.mImageView.setTag(position);
							
		filename = "images/albumslist/"+mAlbums.get(position).getImage();
		byteBuffer = FileUtil.readFileFromAssets(mContext, filename);
		mBitmap = BitmapFactory.decodeByteArray(byteBuffer, 0, byteBuffer.length);
		holder.mImageView.setImageBitmap(mBitmap);
		holder.mTextView.setText(mAlbums.get(position).getTitle());
		holder.mTextView.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Intent mIntent = new Intent(mContext,AlbumGridActivity.class);
				mIntent.putExtra("ID",mAlbums.get(position).getID());
				mIntent.putExtra("TITLE", mAlbums.get(position).getTitle());
				mIntent.putExtra("IMAGE", mAlbums.get(position).getTitle());
				mContext.startActivity(mIntent);
			}
		});
		
		byteBuffer = null;
		return view;
		
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return mAlbums.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	

}
